home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 24 / Mac Magazin and MacEasy Magazine CD - Issue 24.iso / Grafik & Text / Word Fixer ƒ / Word Fixer.c < prev    next >
C/C++ Source or Header  |  1996-07-13  |  9KB  |  373 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <Processes.h>
  4. #include <Script.h>
  5. #include <Folders.h>
  6.  
  7.  
  8.  
  9. /*
  10.  
  11.  
  12. Directions for building word fixer:
  13.  
  14. •  To make an extension to go on the loadset, change the file type to 'appe'. It
  15. will run in quiet mode, without any feedback.
  16.  
  17. •  To make an application for users to run (for example if they're copying it off
  18. the keyserver) change the file type to 'APPL'.  It checks a few more things, 
  19. makes sure Word isn't running, and also kills the RegDBs (which the appe doesn't do).
  20. It also posts a notification as to whether it ran successfully or not.
  21.  
  22.  
  23. ps: don't look too carefully at this code, most of it is what Ed whipped up on
  24. an emergency basis and i only made a few minor cleanups.  it's more than a little
  25. bit skanky.  have fun.
  26.  
  27.            -drew 7/12/96
  28.  
  29.  
  30. */
  31.  
  32.  
  33.  
  34. // global flag -- are we a real application?
  35. Boolean        isAPPL;
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. Boolean FindAProcess (OSType signature, ProcessSerialNumber
  46.                                                 *process,ProcessInfoRec *infoRec,
  47.                                                 FSSpecPtr aFSSpecPtr);
  48.  
  49. Boolean FindAProcess (OSType signature, ProcessSerialNumber *process,
  50.                                                 ProcessInfoRec *infoRec, FSSpecPtr
  51.                                                 aFSSpecPtr)
  52. {
  53.     process->highLongOfPSN = 0;
  54.     process->lowLongOfPSN = kNoProcess;     // start from the beginning
  55.  
  56.     infoRec->processInfoLength = sizeof(ProcessInfoRec);
  57.     infoRec->processName = (StringPtr) NewPtr(32);
  58.     infoRec->processAppSpec = aFSSpecPtr;
  59.  
  60.     while (!GetNextProcess(process)) {
  61.  
  62.         if (!GetProcessInformation(process, infoRec) ) {
  63.             if ( (infoRec->processType == (long) 'APPL') &&
  64.                  (infoRec->processSignature == signature) )
  65.                 return TRUE;        // found the process
  66.  
  67.         }
  68.     } // while
  69.  
  70.     return FALSE;
  71. }
  72.  
  73.  
  74.  
  75. // function prototype
  76. long  GetMicrosoftDirID(void);
  77. long  GetMicrosoftDirID(void)
  78. {
  79.     #define kMaxMatches 1            // find up to 1 match in one pass
  80.     #define kOptBufferSize 16384     // use a 16k search cache for speed
  81.     
  82.     OSErr gErr;     // error variable
  83.     HParamBlockRec gPb; // parameter block for PBCatSearch
  84.     FSSpec gTheResults[kMaxMatches];     // put matches here
  85.     CInfoPBRec gSpec1;     // search criteria, part 1
  86.     CInfoPBRec gSpec2;     // search criteria, part 2
  87.     char gBuffer[kOptBufferSize];     // search cache
  88.     Str255 gFileName;    // name of string to look for
  89.     short vRefNum = -1;    // volume on which to search    
  90.     short loopy,done;   // set to true when all matches done
  91.     
  92.     
  93.     // search for temp
  94.     // first use strcpy to copy string to gFileName, then convert to pascal style
  95.     // string for toolbox use
  96.     strcpy ((char *)gFileName, "MS Spelling");
  97.     CtoPstr((char*)gFileName);
  98.  
  99.     gPb.csParam.ioCompletion = nil;  // no completion routine
  100.     gPb.csParam.ioNamePtr = nil;    // no volume name;  use vRefNum
  101.     gPb.csParam.ioVRefNum = vRefNum;  // volume to search
  102.     gPb.csParam.ioMatchPtr = gTheResults;   // points to results buffer
  103.     gPb.csParam.ioReqMatchCount = kMaxMatches;   // number of matches
  104.  
  105.     // search partial name, file attributs, and creation date
  106.     gPb.csParam.ioSearchBits = fsSBPartialName + fsSBFlAttrib;
  107.     gPb.csParam.ioSearchInfo1 = &gSpec1;   // points to first criteria set
  108.     gPb.csParam.ioSearchInfo2 = &gSpec2;   // points to second criteria set
  109.     gPb.csParam.ioSearchTime = -1;            // don't time out on searches
  110.     gPb.csParam.ioCatPosition.initialize = 0;   // set hint to 0
  111.     gPb.csParam.ioOptBuffer = gBuffer;    // point to search cache
  112.     gPb.csParam.ioOptBufSize  = kOptBufferSize;   // size of search cache
  113.  
  114.     gSpec1.hFileInfo.ioNamePtr = gFileName;   // point to string to find
  115.     gSpec1.hFileInfo.ioFlAttrib = 0x00;    // clear bit 4 to ask for files
  116.     
  117.     gSpec2.hFileInfo.ioNamePtr = nil;   // set to nil
  118.     gSpec2.hFileInfo.ioFlAttrib  = 0x10;  // set mask for bit 4
  119.     
  120.     done = 0;
  121.     do{
  122.     gErr = PBCatSearchSync((CSParam*)&gPb);    // get some files
  123.     done = (gErr == eofErr);     // eofErr returned when all done
  124.     if (((gErr == noErr) || done) && (gPb.csParam.ioActMatchCount > 0) )
  125.         for ( loopy = 0;  loopy < gPb.csParam.ioActMatchCount; loopy++) {
  126.             // report all matches found
  127.             return gTheResults[loopy].parID;
  128.         }
  129.     }while(!done);
  130.     
  131.     return 0L;
  132. }
  133.  
  134.  
  135. void GetHardDiskName(StringPtr name);
  136. void GetHardDiskName(StringPtr name)
  137. {
  138.     CInfoPBRec    pb;    
  139.     
  140.     pb.dirInfo.ioCompletion = nil;
  141.     pb.dirInfo.ioNamePtr = name;
  142.     pb.dirInfo.ioVRefNum = -1;
  143.     pb.dirInfo.ioFDirIndex = -1;
  144.     pb.dirInfo.ioDrDirID = 2;
  145.     PBGetCatInfo(&pb,false);
  146. }
  147.  
  148.  
  149.  
  150.  
  151. Boolean        notificationDone = false;
  152.  
  153. pascal void ErrorNotifyDone( NMRec *nmr );
  154. pascal void ErrorNotifyDone( NMRec *nmr )
  155. {
  156.     long    oldA5 = SetA5( nmr->nmRefCon );
  157.     
  158.     NMRemove( nmr );
  159.     notificationDone = true;
  160.     
  161.     SetA5( oldA5 );
  162. }
  163.  
  164.  
  165. void ErrorNotify( short id );
  166. void ErrorNotify( short id )
  167. {
  168.     NMRecPtr    nmr;
  169.     Str255        string;
  170.     EventRecord    ev;
  171.     NMUPP        nmupp;
  172.  
  173.     if (!isAPPL)
  174.         return;
  175.     
  176.     GetIndString( string, 128, id );
  177.     
  178.     nmr = (NMRecPtr) NewPtrSysClear( sizeof(NMRec) );
  179.     if (!nmr) return;
  180.     
  181.     nmr->qType = nmType;
  182.     nmr->nmStr = string;
  183.     nmr->nmResp = (nmupp = NewNMProc(ErrorNotifyDone));
  184.     nmr->nmRefCon = GetCurrentA5();
  185.     
  186.     if (NMInstall(nmr))
  187.         return;
  188.         
  189.     while (!notificationDone)
  190.         WaitNextEvent( everyEvent, &ev, 60, nil );
  191.  
  192.     DisposeRoutineDescriptor( nmupp );
  193.     DisposePtr( (Ptr)nmr );
  194. }
  195.  
  196.  
  197. void MicrosoftProgrammersAreIdiots( Handle data, char *hdName );
  198. void MicrosoftProgrammersAreIdiots( Handle data, char *hdName )
  199. {
  200.     char    c[200];
  201.     short    len, index, copy;
  202.     
  203.     len = sprintf(c,"%s",hdName);
  204.     c[len++] = 6;
  205.     c[len++] = 7;
  206.     c[len++] = 8;
  207.     
  208.     for (copy = 0,index = 0;index<GetHandleSize(data);index++)
  209.     {
  210.         if (copy)
  211.             c[len++] = ((char*)(*data))[index];
  212.         
  213.         if (((char*)(*data))[index] == 8)
  214.             copy = 1;
  215.     }
  216.  
  217.     c[len++] = 0;
  218.     CtoPstr(c);
  219.     SetHandleSize(data,1+c[0]);   /* IM II pg 34 */
  220.     BlockMove(c,*data,1+c[0]);
  221.     ChangedResource(data);
  222.     WriteResource(data);
  223.     ReleaseResource(data);
  224. }
  225.  
  226.  
  227.  
  228.  
  229. Boolean AmIReal( void );
  230. Boolean AmIReal( void )
  231. {
  232.     ProcessSerialNumber    myPSN;
  233.     ProcessInfoRec        info;
  234.     FSSpec                spec;
  235.     FInfo                finfo;
  236.     
  237.     GetCurrentProcess( &myPSN );
  238.     
  239.     info.processInfoLength = sizeof(info);
  240.     info.processName = nil;
  241.     info.processAppSpec = &spec;
  242.     GetProcessInformation( &myPSN, &info );
  243.     
  244.     FSpGetFInfo( &spec, &finfo );
  245.     
  246.     return ( finfo.fdType == 'APPL' );
  247. }
  248.  
  249.  
  250.  
  251. void main( void )
  252. {
  253.     long        dirID;
  254.     FSSpec        spec;
  255.     short        prefVRef;
  256.     long        prefDirID,index;
  257.     char        hdName[32];
  258.     char        c[256];
  259.     short        len,fRefNum,copy;
  260.     Handle        data;
  261.     
  262.     
  263.     isAPPL = AmIReal();
  264.     
  265.     
  266.     GetHardDiskName((StringPtr)hdName);
  267.     PtoCstr((StringPtr)hdName);
  268.     
  269.     if (isAPPL)
  270.     {
  271.         ProcessSerialNumber    psn;
  272.         ProcessInfoRec        procInfo;
  273.         if (FindAProcess('MSWD',&psn,&procInfo,&spec))
  274.         {
  275.             ErrorNotify( 2 );
  276.             ExitToShell();
  277.         }
  278.     }
  279.     
  280.  
  281.     dirID = GetMicrosoftDirID();
  282.     if (!dirID)
  283.     {
  284.         ErrorNotify( 1 );
  285.         ExitToShell();
  286.     }
  287.     
  288.     FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,&prefVRef,&prefDirID);   /* IM VI chap. 9 pg 44 */
  289.  
  290. /*
  291.     // delete the regDBs -- this stopped being necessary (in fact it was bad)
  292.     //    after i wrote the Microsoft Installer Patch.
  293.     
  294.     if (isAPPL)
  295.     {
  296.         FSMakeFSSpec(prefVRef,prefDirID,"\pRegistration Database",&spec);
  297.         FSpDelete(&spec);
  298.     
  299.         FSMakeFSSpec(prefVRef,prefDirID,"\pPPC Registration Database",&spec);
  300.         FSpDelete(&spec);
  301.     }
  302. */
  303.  
  304.     if (FSMakeFSSpec(prefVRef,prefDirID,"\pWord Settings (6)",&spec))
  305.         FSpCreateResFile(&spec,'MSWD','pref',smSystemScript);
  306.     
  307.     fRefNum = FSpOpenResFile(&spec,fsRdWrPerm);
  308.     if (fRefNum != -1)
  309.     {
  310.         //////////////////////////////////
  311.         
  312.         data = Get1NamedResource('STR ',"\pMicrosoft Word:TOOLS-PATH");
  313.         if (data)
  314.             RmveResource(data);
  315.             
  316.         len = sprintf(c,"%s",hdName);
  317.         c[len++] = 6;
  318.         c[len++] = 7;
  319.         c[len++] = 8;
  320.         c[len++] = 0;
  321.         sprintf(c+strlen(c),"%lx",dirID);
  322.         CtoPstr(c);
  323.         data = NewHandle(c[0]+1);
  324.         BlockMove(c,*data,c[0]+1);
  325.     
  326.         AddResource(data,'STR ',Unique1ID('STR '),"\pMicrosoft Word:TOOLS-PATH");
  327.         WriteResource(data);
  328.         ReleaseResource(data);
  329.         
  330.         //////////////////////////////////
  331.  
  332.         data = Get1NamedResource('STR ',"\pMicrosoft Word:INI-PATH");
  333.         if (data)
  334.             MicrosoftProgrammersAreIdiots( data, hdName );
  335.         
  336.         data = Get1NamedResource('STR ',"\pMicrosoft Word:DOC-PATH");
  337.         if (data)
  338.             MicrosoftProgrammersAreIdiots( data, hdName );
  339.             
  340.         data = Get1NamedResource('STR ',"\pMicrosoft Word:WORKGROUP-DOT-PATH");
  341.         if (data)
  342.             MicrosoftProgrammersAreIdiots( data, hdName );
  343.             
  344.         data = Get1NamedResource('STR ',"\pMicrosoft Word:PICTURE-PATH");
  345.         if (data)
  346.             MicrosoftProgrammersAreIdiots( data, hdName );
  347.             
  348.         data = Get1NamedResource('STR ',"\pMicrosoft Word:STARTUP-PATH");
  349.         if (data)
  350.             MicrosoftProgrammersAreIdiots( data, hdName );
  351.         
  352.         data = Get1NamedResource('STR ',"\pMicrosoft Word:AUTOSAVE-PATH");
  353.         if (data)
  354.             MicrosoftProgrammersAreIdiots( data, hdName );
  355.  
  356.         data = Get1NamedResource('STR ',"\pMicrosoft Word:USER-DOT-PATH");
  357.         if (data)
  358.             MicrosoftProgrammersAreIdiots( data, hdName );
  359.             
  360.         CloseResFile(fRefNum);
  361.         
  362.         ErrorNotify( 4 );
  363.     }
  364.     else
  365.     {
  366.         ErrorNotify( 3 );
  367.         ExitToShell();
  368.     }
  369. }
  370.  
  371.  
  372.  
  373.